package com.ruoyi.project.system.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Anonymous;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.blog.service.IBIndexService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.config.RuoYiConfig;

import javax.annotation.Resource;

/**
 * 首页
 *
 * @author ruoyi
 */
@RestController
public class SysIndexController
{
    /** 系统基础配置 */
    @Autowired
    private RuoYiConfig ruoyiConfig;

    @Resource
    private IBIndexService indexService;
    /**
     * 访问首页，提示语
     */
    @RequestMapping("/")
    public String index()
    {
        return StringUtils.format("欢迎使用{}后台管理框架，当前版本：v{}，请通过前端地址访问。", ruoyiConfig.getName(), ruoyiConfig.getVersion());
    }
    @Anonymous
    @GetMapping(value = "/getIndexStatistics")
    @ApiOperation(value = "获取首页统计", httpMethod = "GET", response = AjaxResult.class, notes = "获取首页统计")
    public AjaxResult getIndexStatistics() {
        //判断是否是管理员，管理员查看所有数据，否则值查看自己的数据
        boolean admin = SecurityUtils.isAdmin(SecurityUtils.getUserId());
        String userId =null;
        if(!admin){
            userId = SecurityUtils.getUserId().toString();
        }
        return indexService.getIndexStatistics(userId);
    }
    @GetMapping(value = "/initCategory")
    public AjaxResult init() {
        //判断是否是管理员，管理员查看所有数据，否则值查看自己的数据
        boolean admin = SecurityUtils.isAdmin(SecurityUtils.getUserId());
        String userId =null;
        if(!admin){
            userId = SecurityUtils.getUserId().toString();
        }
        return AjaxResult.success(indexService.initCategory(userId));
    }
}
